﻿namespace PowerSong.SongDatabase.ObjectModel
    {
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Provides access to licenses defined in the database. It is possible to create, retrieve, update and delete licenses.
    /// </summary>
    public class Licenses : ObjectModelBase
        {

        internal Licenses(Database database) : base(database) { }

        /// <summary>
        /// Gets a list of all of the available licenses defined in this database.
        /// </summary>
        /// <returns>All of the available licenses.</returns>
        public License[] GetAllLicenses()
            {

            var result = new List<License>();
            foreach (var nextLicense in Database.Filter<License>())
                {
                result.Add(nextLicense);
                }
            return result.ToArray();

            }

        /// <summary>
        /// Determines the number of available licenses defined in the database.
        /// </summary>
        /// <returns>The number of available licenses in the database.</returns>
        public int Count()
            {
            return Database.Filter<License>().Count;
            }

        /// <summary>
        /// Gets the license that has the given ID.
        /// </summary>
        /// <param name="identifier">The ID of the license to retrieve.</param>
        /// <returns>The requested license, or NULL if there does not exist a license with the given ID.</returns>
        public License Get(Guid identifier)
            {
            return Database.Get<License>(identifier);
            }

        /// <summary>
        /// Determines whether or not the given license exists in the database.
        /// </summary>
        /// <param name="songID">The ID of the license to search for in the database.</param>
        /// <returns>True, if the license exists in the database. False otherwise.</returns>
        public bool Contains(Guid licenseID)
            {
            return Get(licenseID) != null;
            }

        /// <summary>
        /// Determines whether or not the given license exists in the database.
        /// </summary>
        /// <param name="name">The name of the license.</param>
        /// <param name="caseSensitive">Indicates whether or not to perform the search in a case sensitive way.</param>
        /// <returns>True, if there exists in this database a license with the given name, otherwise False.</returns>
        public bool Contains(string name, bool caseSensitive = false)
            {
            foreach (var license in Database.Filter<License>())
                {
                if (caseSensitive)
                    {
                    if (license.Name == name) return true;
                    }
                else
                    {
                    if (license.Name.ToUpper() == name.ToUpper()) return true;
                    }
                }
            return false;
            }

        /// <summary>
        /// Creates a new license, or effectively edits an existing license.
        /// To create a license, supply an instance of the License class that has been constructed.
        /// To edit an existing license, retrieve it from the database first, modify its fields, then supply that instance instead.
        /// </summary>
        /// <param name="license">The license to write to the database.</param>
        public void Write(License license)
            {
            Database.Write(license);
            }

        /// <summary>
        /// Deletes an existing license from the database.
        /// </summary>
        /// <param name="song">The license to delete from the database.</param>
        public void Delete(License license)
            {
            Database.Delete(license);
            }

        }

    }
